package goods_manage.handler;

import goods_manage.entity.GoodsDo;
import goods_manage.service.GoodsService;
import goods_manage.service.impl.GoodsServiceImpl;
import goods_manage.show.MainView;
import goods_manage.show.view.AddGoodsView;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class AddGoodsViewHandler implements ActionListener {
    private AddGoodsView addGoodsView;
    private MainView mainView;
    public AddGoodsViewHandler(AddGoodsView addGoodsView, MainView mainView){
        this.addGoodsView = addGoodsView;
        this.mainView=mainView;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        JButton jButton=(JButton) e.getSource();
        String text=jButton.getText();
        if("添加".equals(text)){
            GoodsService goodsService =new GoodsServiceImpl();
            GoodsDo goodsDo = addGoodsView.buildGoods();
            boolean addResult= goodsService.add(goodsDo);
            if(addResult){
                //更新表格数据
                mainView.reloadTable();
                JOptionPane.showMessageDialog(addGoodsView,"添加成功");
                addGoodsView.dispose();
            }else{
                JOptionPane.showMessageDialog(addGoodsView,"添加失败");
            }
        }
    }
}
